<template>
  <!-- 采购计划关联采购单明细 -->
  <div class="table-w">
    <a-table class="table-two-line" :span-method="spanMethodAll" ref="tableRef" :bordered="false" :data="tableList" :scroll="{ y: '100%' }" size="mini" :pagination="false">
      <template #columns>
        <a-table-column data-index="receiptNo" title="入库订单" :width="90" fixed="left">
          <template #cell="{ record }">
            <p class="highlight">{{ record.receiptNo }}</p>
          </template>
        </a-table-column>
        <a-table-column data-index="headUname" title="仓库" :width="120" fixed="left">
          <template #cell="{ record }">
            <p>{{ record.warehouseName }}({{ record.warehouseCode }})</p>
          </template>
        </a-table-column>
        <a-table-column data-index="img" title="图片" :width="60" fixed="left">
          <template #cell="{ record }">
            <ImgPopover :src="record.productImg">
              <template #con>
                <a-image width="40" height="40" fit="contain" :src="record.productImg" />
              </template>
            </ImgPopover>
          </template>
        </a-table-column>
        <a-table-column title="产品名称" :width="120" fixed="left">
          <template #cell="{ record }">
            <TextLimit>{{ record.productName }}</TextLimit>
          </template>
        </a-table-column>
        <a-table-column data-index="sku" title="SKU/类目" :width="120" fixed="left">
          <template #cell="{ record }">
            <TextLimit :rows="1">{{ record.sku || '--' }}</TextLimit>
            <p>{{ record.cateName || '--' }}</p>
          </template>
        </a-table-column>
        <a-table-column data-index="msku" title="MSKU/ASIN" :width="120" fixed="left">
          <template #cell="{ record }">
            <TextLimit :rows="1">{{ record.msku || '--' }}</TextLimit>
            <TextLimit :rows="1">{{ record.asin || '--' }}</TextLimit>
          </template>
        </a-table-column>
        <a-table-column data-index="warehouseRealAmt" title="已入库数" :width="80" />
        <a-table-column data-index="warehouseReturnAmt" title="退回数" :width="80" />
        <a-table-column data-index="rate" title="税率" :width="80">
          <template #cell="{ record }">
            <p>{{ record.rate }}%</p>
          </template>
        </a-table-column>
        <a-table-column data-index="price" title="含税单价" :width="80">
          <template #cell="{ record }">
            <p>￥{{ record.price }}</p>
          </template>
        </a-table-column>
        <a-table-column data-index="inventoryValue" title="入库货值" :width="80">
          <template #cell="{ record }">
            <p>￥{{ record.inventoryValue }}</p>
          </template>
        </a-table-column>
        <a-table-column data-index="locationCode" title="入库货位" :width="100" />
      </template>
    </a-table>
  </div>
</template>
<script lang="ts" setup>
  import { ref, onMounted } from 'vue'
  import { findReceiptItemByPlan } from '@/api/purchase'

  const props = defineProps({
    currentData: {
      type: Object,
      default: () => {},
    },
  })
  onMounted(() => {
    getTableList()
  })

  const tableList = ref<Array<any>>([])
  const tableLoading = ref(false)
  const dataMap = ref<any>({})

  async function getTableList() {
    tableLoading.value = true
    const res: any = await findReceiptItemByPlan({
      id: props.currentData.id,
    })
    tableLoading.value = false
    dataMap.value = res?.data || {}
    let list: Array<any> = []
    Object.keys(dataMap.value).forEach((key: string) => {
      dataMap.value[key].forEach((el: any, i: number) => {
        el.index = i
        list.push(el)
      })
    })
    tableList.value = list
  }
  // 合并单元格
  function spanMethodAll(option: any) {
    let { column, record } = option
    if (column.dataIndex === 'receiptNo' && record.index === 0) {
      return { rowspan: dataMap.value[record.receiptNo]?.length || 1 }
    }
  }
</script>
<style lang="less" scoped>
  .table-w {
    flex: 1;
    overflow: hidden;
  }
</style>
